import Vue from "vue";
import VueRouter from "vue-router";
import Layout from '@/views/layout/Index.vue';
import Login from '@/views/login/Index.vue';
import Home from '@/views/home/Index.vue';

import Product from '@/views/product/Index.vue';
import ProductList from '@/views/product/list/Index.vue';
import Category from '@/views/product/category/Index.vue';
import AddProduct from '@/views/product/list/AddProduct.vue'

import Order from '@/views/order/Index.vue';
import OrderList from '@/views/order/list/Index.vue';
import Collect from '@/views/order/collect/Index.vue';
import Contract from '@/views/order/contract/Index.vue';

import Advert from '@/views/advert/Index.vue';
import AdvertList from '@/views/advert/list/Index.vue';

import SystemManage from '@/views/SystemManage/Index.vue';
<<<<<<< HEAD

=======
import department from '@/views/SystemManage/department/Index.vue';
import role from '@/views/SystemManage/role/Index.vue';
>>>>>>> e558facf022688ce3d835b65714b1b14b7cb08fa

Vue.use(VueRouter);

const routes = [
  {
    path: '/',
    component: Layout,
    meta: {
      title: '首页',
      isLogin: true
    },
    // 二级路由
    children: [
      {
        path: '/',
        name: 'home',
        component: Home,
        meta: {
          title: '首页'
        }
      },
      {
        path: '/product',
        name: 'product',
        component: Product,
        redirect: '/product/list',//重定向
        meta: {
<<<<<<< HEAD
          title: '产品管理'
=======
          title: '商品管理'
>>>>>>> e558facf022688ce3d835b65714b1b14b7cb08fa
        },
        children: [
          {
            path: 'list',
            name: 'list',
            component: ProductList,
            meta: {
<<<<<<< HEAD
              title: '产品列表'
=======
              title: '商品列表'
>>>>>>> e558facf022688ce3d835b65714b1b14b7cb08fa
            }
          },
          {
            path: 'category',
            name: 'category',
            component: Category,
            meta: {
<<<<<<< HEAD
              title: '产品分类'
=======
              title: '商品分类'
>>>>>>> e558facf022688ce3d835b65714b1b14b7cb08fa
            }
          },
          {
            path: 'add-product', //商品添加 
            name: 'addProduct',
            component: AddProduct,
            meta: {
              activeMenu: '/product/list',
              title: '商品详情'
            }
          }
        ]
      },
      {
        path: '/order',
        name: 'order',
        component: Order,
        redirect: '/order/list',//重定向
        meta: {
          title: '订单管理'
        },
        children: [
          {
            path: 'list',
            name: 'orderList',
            component: OrderList,
            meta: {
              title: '订单列表'
            }
          },
          {
            path: 'collect',
            name: 'collect',
            component: Collect,
            meta: {
              title: '订单汇总'
            }
          },
          {
            path: 'contract',
            name: 'contract',
            component: Contract,
            meta: {
              title: '订单审核'
            }
          },
        ]
      },
      {
        path: '/advert',
        name: 'advert',
        component: Advert,
        redirect: '/advert/list',//重定向
        meta: {
          title: '广告管理'
        },
        children: [
          {
            path: 'list',
            name: 'advertList',
            component: AdvertList,
            meta: {
              title: '广告列表'
            }
          }
        ]
      },
<<<<<<< HEAD
     
=======
      {
        path: "/system",
        name: "system",
        component: SystemManage,
        redirect: "/system/role",
        meta: {
          title: "系统管理",
        },
        children: [
          {
            path: "role",
            name: "role",
            component: role,
            meta: {
              title: "角色管理",
            },
          },
          {
            path: "department",
            name: "department",
            component: department,
            meta: {
              title: "部门管理",
            },
          },
        ],
      }
>>>>>>> e558facf022688ce3d835b65714b1b14b7cb08fa
    ]
  },
  {
    path: '/login',
    name: 'login',
    component: Login,
  },
  
]

const router = new VueRouter({
  mode: 'hash',
  // mode: 'history', 
  // // base: process.env.NODE_ENV=='production'?'./':'/',
  // base:'/',
  routes
});

// 针对ElementUI导航栏中重复导航报错问题
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}


import store from '@/store'
router.beforeEach((to, from, next) => {
  //根据字段判断是否路由过滤
  if (to.matched.some(ele => ele.meta.isLogin)) {
    if (store.state.login.userinfo.token) {
      next()
    } else {
      next('/login')
    }
  }
  else {
    next()
  }
})

export default router;
